package com.songVersion.controller.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.songVersion.model.Cover;
import com.songVersion.model.ThumbnailCache;
import com.songVersion.model.Version;
import com.songVersion.model.YoutubeInfo;
import com.songVersion.services.CoverManager;
import com.songVersion.services.VersionManager;
import com.songVersion.services.impl.CoverManagerImpl;
import com.songVersion.services.impl.VersionManagerImpl;
import com.songVersion.youtube.YoutubeSearch;

public class DetailAction extends GeneralAction
{
	public static int MAX_RESULTS_PER_SECTION = 10;

	private String videoId;
	

	public DetailAction()
	{
		super();
	}

	private void getOriginalInYoutube()
	{
		YoutubeSearch ys = new YoutubeSearch();
		YoutubeInfo info = ys.searchVideo(getVideoId(), getSongName(), getSongAuthor());
		if (info != null)
		{
			setAttribute("originalSong", info);
			saveSong(new ThumbnailCache(info.getThumbnails().get(0)));

			// ve.getId()
			setAttribute("songName", getSongName());
		} else
		{
			setAttribute("message", "no se ha encontrado " + getSongName());
		}
	}
	

	private void saveSong(ThumbnailCache thumb)
	{
		String author = getSongAuthor();
		if (author == null || author.equals(""))
		{
			CoverManagerImpl cm = new CoverManagerImpl();
			Cover c = new Cover(getSongName(), thumb);
			boolean isParody = (Boolean)getReq().getAttribute("isParody");
			c.setIsParody(isParody);
			System.out.println("isparody:" + c.getIsParody());
			cm.setTheCover(c);
			cm.save();
			System.out.println(cm.getTheCover().getIsParody());
		}else
		{
			VersionManager vm = new VersionManagerImpl();
			vm.setTheVersion(new Version(getSongName(), getSongAuthor(), thumb));
			vm.save();
		}
		
		
	}

	@Override
	public void init(HttpServletRequest req)
	{
		super.init(req);
		String pVideoId =getParameter("videoId"); 
		if ( pVideoId != null)
		{
			setVideoId(pVideoId);
		}
			
	}
	private void getCoversInYoutube()
	{

		YoutubeSearch ys = new YoutubeSearch();
		List<YoutubeInfo> vf = ys.doSearchCover(getSongName(),
				MAX_RESULTS_PER_SECTION);
		setAttribute("covers", vf);
	}

	private void getParodiesInYoutube()
	{

		YoutubeSearch ys = new YoutubeSearch();
		List<YoutubeInfo> vf = ys.doSearchParody(getSongName(),
				MAX_RESULTS_PER_SECTION);
		setAttribute("covers", vf);
	}



	public String process()
	{
		
		boolean isSearch = (getSongName() != null && !"".equals(getSongName()));
		String tabView = getReq().getParameter("tabView");
		String view = "/views/main.jsp";
		if (isSearch)
		{
			if (tabView == null || "".equals(tabView))
			{
				getOriginalInYoutube();
//				int numVersions = 
					getVersionsInNest();
//				if (numVersions > 0)
	//				saveSearch();
			} else if (tabView.equals("versions"))
			{
				int pPage = getIntParameter("page");
				setPage(pPage);
				getVersionsInNest();
				view = "/views/versions.jsp";
			} else if (tabView.equals("covers"))
			{
				getCoversInYoutube();
				view = "/views/covers.jsp";
			} else if (tabView.equals("parodies"))
			{
				getParodiesInYoutube();
				view = "/views/parodies.jsp";

			}
		} else
		{
			getMaxSearched();
		}
		setAttribute("isSearch", isSearch);
		return view;

	}

	public String getVideoId()
	{
		return videoId;
	}

	public void setVideoId(String videoId)
	{
		this.videoId = videoId;
	}



}
